/**
 * 
 */
package com.acc.softmgr.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.business.model.SysUserMenu;
import com.acc.softmgr.business.model.SysUserMenuExample;
import com.acc.softmgr.business.service.ISysUserMenuService;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.ResponseObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月12日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("sysUserMenu")
public class SysUserMenuController {

	@Autowired
	private ISysUserMenuService sysUserMenuService;

	@RequestMapping(value = "searchSelectUserByMenuIdAndUserId", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchSelectUserByMenuIdAndUserId(@Validated Integer menuId, Integer userId)
			throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (menuId == null || menuId == 0 || userId == null || userId == 0) {
			responseObject.setStatus(RestStatus.SERVER_ERROR.code);
			responseObject.setMessage(RestStatus.SERVER_ERROR.message);
			return responseObject;
		}
		SysUserMenuExample example = new SysUserMenuExample();
		example.getCriteria().andMenu_idEqualTo(menuId);
		example.getCriteria().andUser_idEqualTo(userId);
		List<SysUserMenu> sysUserMenus = sysUserMenuService.selectByExample(example);
		if (sysUserMenus != null && sysUserMenus.size() > 0) {
			responseObject.setStatus(RestStatus.SUCCESS.code);
			responseObject.setMessage(RestStatus.SUCCESS.message);
			return responseObject;
		}
		responseObject.setStatus(RestStatus.SERVER_ERROR.code);
		responseObject.setMessage(RestStatus.SERVER_ERROR.message);
		return responseObject;
	}

	/**
	 * 通过菜单关联批量用户
	 * 
	 * @param menuId
	 * @param userIds
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "link", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject link(@Validated Integer menuId, String userIds) throws Exception {
		ResponseObject responseObject = new ResponseObject();

		String[] users = userIds.split(",");
		if (StringUtil.isEmpty(userIds) || menuId == null) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUserMenuExample example = new SysUserMenuExample();
		example.getCriteria().andMenu_idEqualTo(menuId);
		List<SysUserMenu> list = sysUserMenuService.selectByExample(example);
		if (list != null && list.size() > 0) {
			for (SysUserMenu userMenu : list) {
				sysUserMenuService.deleteByPrimaryKey(userMenu.getId());// 删除已经关联用户
			}
		}
		for (String userId : users) {
			SysUserMenu psUserMenu = new SysUserMenu();
			psUserMenu.setMenu_id(menuId);
			psUserMenu.setUser_id(Integer.parseInt(userId));
			sysUserMenuService.insert(psUserMenu);
		}

		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

}
